package com.woniuxy.controller;

import com.woniuxy.model.UserInfo;
import com.woniuxy.result.Result;
import com.woniuxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("queryusers")
    public Result queryUsers() {
        List<UserInfo> userInfos = userService.getAll();
        return Result.success(userInfos);
    }

    //根据id进行查询
    @RequestMapping("querybyid")
    public Result queryById(Integer uid) {
        UserInfo userInfo = userService.getByUserId(uid);
        if(userInfo==null){
            return Result.failed().msg("未找到信息");
        }
        return Result.success(userInfo);
    }

    @PostMapping("insert")
    public Result insert(@RequestBody UserInfo userInfo) {
        userService.addUser(userInfo);
        return Result.success().msg("新增用户成功!");
    }
    //删除用户信息（根据Id批量删除）
    @DeleteMapping("delete")
    public Result delete(@RequestParam String ids){
        userService.deleteBatch(ids);
        return Result.success().msg("删除成功");
    }

    //修改用户信息
    @PutMapping("update")
    public Result modify(@RequestBody UserInfo userInfo){
        userService.modifyUser(userInfo);
        return Result.success().msg("修改成功");
    }
}